package com.frxuexi.repair.view

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.repair.R
import com.frxuexi.repair.RepairIntent
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.adapter.RepairImgAdapter
import com.frxuexi.repair.databinding.ActivityRepairAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairAddActivity")
@AndroidEntryPoint
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding,RepairIntent,RepairViewModel>() {
    override val binding: ActivityRepairAddBinding by lazy { ActivityRepairAddBinding.inflate(layoutInflater) }
    override val vm: RepairViewModel by viewModels<RepairViewModel>()
    val repairImgAdapter by lazy { RepairImgAdapter() }
    var areaName="A区停车场"
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        ToastUtils.showLong("报修完成")
                        finish()
                    }
                    is  UiState.onError->{
                        Log.i("lr",it.msg)
                    }
                    else->{}
                }
            }
        }
    }

    override fun initView() {
        binding.headRepairAdd.apply {
            headBack.setOnClickListener {
                finish()
            }
            headTitle.setText("维修添加")
            headAdd.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
            headAdd.setOnClickListener {
                val currentTime = System.currentTimeMillis()
                val phone = binding.repairaddPhone.text.toString()
                val remark = binding.repairaddRemark.text.toString()
                val user = binding.repairaddUser.text.toString()
                val item = RepairBeanItem(
                    phone,
                    remark,
                    "",
                    null,
                    "WX${vm.getDataTimeString(currentTime)}",
                    0,
                    areaName,
                    user,
                    vm.getDataTime(currentTime),
                    SPUtils.getInstance().getInt(Const.UID)
                )
                vm.sendIntent(RepairIntent.addRepair(item))
            }
        }

        binding.repairaddRv.apply {
            adapter=repairImgAdapter
            layoutManager=GridLayoutManager(this@RepairAddActivity,3)
        }

        binding.repairaddImg.setOnClickListener {
            if(repairImgAdapter.data.size==4){
                ToastUtils.showLong("只能传4张")
                return@setOnClickListener
            }
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.repairaddUser.setText(SPUtils.getInstance().getString(Const.UNAME))

        val areaList = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areaList)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairaddLocarion.adapter=areaAdapter
        binding.repairaddLocarion.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
//                areaId=position+1
                areaName=areaList[position]
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(bean?.path!!)
            repairImgAdapter.notifyDataSetChanged()

        }
    }
}